#!/bin/bash
# bash <(curl -fsSl https://gitee.com/Astro-Lee/storage/raw/master/install.sh)
# 安装 astrometry.net, sextractor, scamp, swarp, psfex.
# astrometry.net 参考 http://plaidhat.com/code/astrometry080-centos82.php
# http://astrometry.net/doc/readme.html
CWD=$(pwd)

# https://docs.fedoraproject.org/en-US/epel/#_el8
sudo subscription-manager repos --enable codeready-builder-for-rhel-8-$(arch)-rpms
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

sudo dnf update -y
sudo dnf install -y cairo-devel
sudo dnf install -y libpng-devel
sudo dnf install -y libjpeg-turbo-devel
sudo dnf install -y zlib-devel
sudo dnf install -y bzip2-devel
sudo dnf install -y swig
sudo dnf install -y python3-devel
sudo dnf install -y atlas-devel
sudo dnf install -y fftw-devel
sudo dnf install -y automake
sudo dnf install -y autoconf
sudo dnf install -y libtool

sudo yum-config-manager --enable PowerTools
sudo dnf install -y netpbm
sudo dnf install -y netpbm-devel
sudo dnf install -y netpbm-progs

sudo dnf install -y epel-release
sudo dnf install -y cfitsio
sudo dnf install -y cfitsio-devel
sudo dnf install -y wcslib*

pip3 install numpy astropy

wget -O astrometry.net-latest.tar.gz "https://ghproxy.com/https://github.com/dstndstn/astrometry.net/releases/download/0.94/astrometry.net-0.94.tar.gz"

wget -O sextractor-2.28.0.tar.gz "https://ghproxy.com/https://github.com/astromatic/sextractor/archive/refs/tags/2.28.0.tar.gz"

wget -O scamp-2.10.0.tar.gz "https://ghproxy.com/https://github.com/astromatic/scamp/archive/refs/tags/v2.10.0.tar.gz"

wget -O swarp-2.41.5.tar.gz "https://ghproxy.com/https://github.com/astromatic/swarp/archive/refs/tags/2.41.5.tar.gz"

wget -O psfex-3.24.2.tar.gz "https://ghproxy.com/https://github.com/astromatic/psfex/archive/refs/tags/3.24.2.tar.gz"

tar -zxvf astrometry.net-latest.tar.gz
tar -zxvf sextractor-2.28.0.tar.gz
tar -zxvf scamp-2.10.0.tar.gz
tar -zxvf swarp-2.41.5.tar.gz
tar -zxvf psfex-3.24.2.tar.gz

tee ${CWD}/astrometry.net-*/util/makefile.netpbm <<-'EOF'
NETPBM_INC ?= -I/usr/include/netpbm
NETPBM_LIB ?= -L/usr/lib64 -lnetpbm
EOF

cd /usr/lib64 && sudo ln -s libnetpbm.so.11 libnetpbm.so

cd ${CWD}/astrometry.net-* && ./configure && make -j4 && make -j4 py && make -j4 extra && sudo make install
cd ${CWD}/sextractor-2.28.0 && sh autogen.sh && ./configure && make -j4 && sudo make install
cd ${CWD}/scamp-2.10.0 && sh autogen.sh && ./configure && make -j4 && sudo make install
cd ${CWD}/swarp-2.41.5 && sh autogen.sh && ./configure && make -j4 && sudo make install
cd ${CWD}/psfex-3.24.2 && sh autogen.sh && ./configure LDFLAGS='-pthread' && make -j4 && sudo make install

# If you're only doing wide-field work, you may be able to get by with these data files. They add up to ~400 MB.
cd ${CWD}/astrometry.net-* && for i in $( seq -w 7 19 ); do wget http://data.astrometry.net/4100/index-41$i.fits; done

# You may need these data files as well / instead. They add up to ~10 GB.
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 47 ); do wget http://data.astrometry.net/4200/index-4202-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 47 ); do wget http://data.astrometry.net/4200/index-4203-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 47 ); do wget http://data.astrometry.net/4200/index-4204-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 11 ); do wget http://data.astrometry.net/4200/index-4205-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 11 ); do wget http://data.astrometry.net/4200/index-4206-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 11 ); do wget http://data.astrometry.net/4200/index-4207-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 8 19 ); do wget http://data.astrometry.net/4200/index-42$i.fits; done

# Most people won't need these data files. They add up to ~24 GB.
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 47 ); do wget http://data.astrometry.net/4200/index-4200-$i.fits; done
cd ${CWD}/astrometry.net-* && for i in $( seq -w 0 47 ); do wget http://data.astrometry.net/4200/index-4201-$i.fits; done

# install index files
cd ${CWD}/astrometry.net-* && sudo make install-indexes
cd ${CWD}

sudo tee -a ~/.bashrc <<'EOF'
export PATH=${PATH}:/usr/local/astrometry/bin
EOF
source ~/.bashrc

echo astrometry.net version $(solve-field --version)
sex --version
scamp --version
swarp --version
psfex --version

rm -rf astrometry.net-latest.tar.gz sextractor-2.28.0.tar.gz scamp-2.10.0.tar.gz swarp-2.41.5.tar.gz psfex-3.24.2.tar.gz